/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.mrp.ppm.entity;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

/**
 *
 * @author Zhang Ying and Zhu Xin
 */
@Entity
public class DailyPlannedProduction implements Serializable {

    // this entity represents demand per product per day
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    // dayIndex is 0, 1, 2, 3, 4 -> mon, tue, wed, thu, fri
    int dayIndex;
    private Timestamp productionDay;
    private Long productionDayL;  //production Day in number of mini seconds
    private Long dailyDemand; // TODO:need to be updated everytime backorder is created
    private Long dailyOTDemand; // need to be updated everytime backorder is created
    @OneToMany(mappedBy="dpp",targetEntity= Reservation.class, cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
    private List<Reservation> reservations = new ArrayList<Reservation>();
    @ManyToOne(targetEntity = Product.class, cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Product product;

    public DailyPlannedProduction() {
    }
    
    public DailyPlannedProduction(int dayIndex) {
        this.dayIndex = dayIndex;
        this.dailyDemand = new Long(0);
        this.dailyOTDemand = new Long(0);
        
    }

    public List<Reservation> getReservations() {
        return reservations;
    }

    public void setReservations(List<Reservation> reservations) {
        this.reservations = reservations;
    }

    public Long getDailyOTDemand() {
        return dailyOTDemand;
    }

    public void setDailyOTDemand(Long dailyOTDemand) {
        this.dailyOTDemand = dailyOTDemand;
    }

   

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Long getDailyDemand() {
        return dailyDemand;
    }

    public void setDailyDemand(Long dailyDemand) {
        this.dailyDemand = dailyDemand;
    }

    public int getDayIndex() {
        return dayIndex;
    }

    public void setDayIndex(int dayIndex) {
        this.dayIndex = dayIndex;
    }

    public Timestamp getProductionDay() {
        return productionDay;
    }

    public Long getProductionDayL() {
        return productionDayL;
    }

    public void setProductionDayL(Long productionDayL) {
        this.productionDayL = productionDayL;
    }

    
    
    public void setProductionDay(Timestamp productionDay) {
        this.productionDay = productionDay;
        setProductionDayL(getProductionDay().getTime());
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getTotalDemand(){
        return getDailyDemand()+getDailyOTDemand();
    }
    
    public Long getReserveredAmount() {
        Long reserved = 0l;
        for(Reservation r : this.reservations) {
            reserved += r.getQuantity();
        }
        return reserved;
    }
    
    public Long getAvailable() {
        if(this.reservations.isEmpty()) {
            return getTotalDemand();
        }
        Long reserved = 0l; //quantity reserved
        for(Reservation r : this.reservations) {
            reserved += r.getQuantity();
        }
        return getTotalDemand()-reserved;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof DailyPlannedProduction)) {
            return false;
        }
        DailyPlannedProduction other = (DailyPlannedProduction) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.mrp.ppm.entity.DailyPlannedProduction[ id=" + id + " ]";
    }
}
